package controllers

import (
	"github.com/robfig/revel"
	"kaibb/app/models"
	"time"
)

type WatchDog struct {
	*revel.Controller
}

func (this WatchDog) ReceiveLoc(phoneNumber, deviceId, addrStr string, x, y float64) revel.Result {
	models.SaveLoc(phoneNumber, deviceId, addrStr, x, y)
	return this.RenderText("true")
}

func (this WatchDog) Cmd() revel.Result {
	return this.RenderText("ping")
}

func (this WatchDog) Cfg(deviceId string) revel.Result {
	revel.INFO.Printf("deviceId:%s 请求更新配置", deviceId)
	cfg := models.GetCfg()
	return this.RenderJson(cfg)
}

func (this WatchDog) UpdateCfg(scanTime int, severNo string) revel.Result {
	revel.INFO.Printf("scanTime:%s severNo:%s请求更新配置", scanTime, severNo)
	models.SaveCfg(scanTime, severNo)
	return this.RenderText("true")
}

func (this WatchDog) UpdateName(deviceId, name string) revel.Result {
	revel.INFO.Printf("deviceId:%s 请求更新名字:%s", deviceId, name)
	err := models.UpdateLocAttribute(deviceId, name)
	if err == nil {
		return this.RenderText("true")
	} else {
		return this.RenderText("false")
	}

}

func (this WatchDog) Ping() revel.Result {
	return this.RenderText("true")
}

func (this WatchDog) Index() revel.Result {
	locs := models.GetLocAttrs()
	return this.Render(locs)
}
func (this WatchDog) DayLocation(name string, day time.Time) revel.Result {

	start := day
	end := day.AddDate(0, 0, 1)
	locations := models.GetLocations(name, start, end)
	revel.INFO.Printf("day: %s end:%s", start, end)
	return this.RenderJson(locations)
}
